package com.anrongtec.zcpt.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.art.base.commonui.IBaseFragment;

/**
 * @functionName:
 * @Package com.art.mp.base
 * @author dongtianhao
 * @date 2016年6月12日 下午4:45:11
 * @version V1.0
 * @Description:基础fragment
 */
public abstract class LazyLoadFragment extends IBaseFragment {

	protected Context mContext;
	// TODO 进行带数据缓存
	protected View contentView;
	/**
	 * contentView是否已经被加载
	 */
	protected boolean isViewCreated = false;
	/**
	 * 是否第一次加载数据，用于避免多次联网加载数据
	 */
	protected boolean isFirstLoad = true;

	protected boolean isVisible;

	/**
	 * 在这里实现Fragment数据的缓加载.
	 * 
	 * @param isVisibleToUser
	 */
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (getUserVisibleHint() || isViewCreated) {
			isVisible = true;
			onVisible();
		} else {
			isVisible = false;
			onInvisible();
		}
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		isViewCreated = true;
		mContext = getActivity();
	}



	protected void onVisible() {
		lazyLoad();
	}

	protected abstract void lazyLoad();

	protected void onInvisible() {
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		mContext = null;
	}
}
